import { baseUrl } from "@/config/baseUrl.js"
/* 拨打电话 */
export function callPhone(mobile) {
	uni.makePhoneCall({
		phoneNumber: mobile, //仅为示例，并非真实的电话号码
		success: function() {
			// console.log("拨打电话成功！")
		},
		fail: function(err) {
			// console.log(err,"拨打电话失败！")
		}
	})
}
/* 复制内容 */
export function clipboard(content) {
	uni.setClipboardData({
		data: content,
		success: function () {
			uni.showToast({
			    title: '复制成功'
			})
		},
	});
}
/* 上传图片 */
export const uploadFilePromise = (url) => {
	return new Promise((resolve, reject) => {
		let header = {'Authorization': 'Bearer' +' ' +uni.getStorageSync('token')}
		uni.uploadFile({
			url: `${baseUrl}oss/file/upload`, // 仅为示例，非真实的接口地址
			filePath: url,
			name: 'file',
			header,
			formData: {
				folderName: '/privacy'
			},
			success: (res) => {
				let data = JSON.parse(res.data);
				if(data.code == 200) {
					resolve({data: data.msg})
				}else {
					uni.showToast({
						title: "上传失败！",
						icon: "error"
					})
					reject();
				}
			},
			fail: err => {
				console.log(err, "--err");
			}
		});
	})
}
/* 转移 img数组格式 返回图片字符串格式 */
export const editStr = e => {
	let str = '';
	e.forEach((item, index) => {
		if (index == e.length - 1) {
			str = str + item.url;
		} else {
			str = str + item.url + ',';
		}
	});
	return str
}
/* 转移 img字符串 返回图片数组格式 */
export const editArr = e => {
	let arr = [];
	if (Array.isArray(e)) {
		e.forEach(item => {
			arr.push({
				url: item
			})
		})
	} else {
		e.split(",").forEach(item => {
			arr.push({
				url: item
			})
		})
	}
	return arr

}
/* 手机号加密 */
export function encryptPhoneNumber(phone) {
	const reg = /(\d{3})\d{4}(\d{4})/; // 正则表达式
	return String(phone).replace(reg, "$1****$2"); // 返回加密后的电话号码
}
// wx支付
export const wxPay = (eve,callBack) => {
	uni.requestPayment({
		provider: 'wxpay',
		timeStamp: eve.timeStamp,
		nonceStr: eve.nonceStr,
		package: eve.package,
		signType: eve.signType,
		paySign: eve.paySign,
		success: function(res) {
			callBack(res)
			uni.showToast({
				title: '微信支付成功',
				icon: 'success'
			});
		},
		fail: function(err) {
			uni.showToast({
				title: '支付失败',
				icon: 'error'
			});
		}
	});
}
